package com.ruoyi.source.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.db.domain.SourceMemberDataInfoTemp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.SourceMemberDataInfoHistoryMapper;
import com.ruoyi.db.domain.SourceMemberDataInfoHistory;
import com.ruoyi.source.service.ISourceMemberDataInfoHistoryService;

/**
 * 线索用户资质信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-01
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class SourceMemberDataInfoHistoryServiceImpl implements ISourceMemberDataInfoHistoryService 
{
    @Autowired
    private SourceMemberDataInfoHistoryMapper sourceMemberDataInfoHistoryMapper;

    /**
     * 查询线索用户资质信息
     * 
     * @param id 线索用户资质信息主键
     * @return 线索用户资质信息
     */
    @Override
    public SourceMemberDataInfoHistory selectSourceMemberDataInfoHistoryById(Long id)
    {
        return sourceMemberDataInfoHistoryMapper.selectSourceMemberDataInfoHistoryById(id);
    }

    @Override
    public List<Map<String, Object>> getCity(SourceMemberDataInfoHistory sourceMemberDataInfoHistory) {
        List<Map<String, Object>> cityCount = sourceMemberDataInfoHistoryMapper.getCity(sourceMemberDataInfoHistory);
        return cityCount;
    }

    @Override
    public List<Map<String, Object>> getCityList(SourceMemberDataInfoHistory sourceMemberDataInfoHistory) {
        List<Map<String, Object>> cityCount = sourceMemberDataInfoHistoryMapper.getCityCount(sourceMemberDataInfoHistory);
        Map<String, List<Map<String, Object>>> provinceCityMap = new HashMap<>();

        for (Map<String, Object> city : cityCount) {
            String province = (String) city.get("province");
            String cityName = (String) city.get("city");
            long value = (Long) city.get("value");

            if (provinceCityMap.containsKey(province)) {
                Map<String, Object> cityMap = new HashMap<>();
                cityMap.put("name", cityName);
                cityMap.put("value", value);

                provinceCityMap.get(province).add(cityMap);
            } else {
                List<Map<String, Object>> cityList = new ArrayList<>();
                Map<String, Object> cityMap = new HashMap<>();
                cityMap.put("name", cityName);
                cityMap.put("value", value);

                cityList.add(cityMap);
                provinceCityMap.put(province, cityList);
            }
        }
        
        List<Map<String, Object>> transformedData = new ArrayList<>();
        for (Map.Entry<String, List<Map<String, Object>>> entry : provinceCityMap.entrySet()) {
            List<Map<String, Object>> cityData = entry.getValue();

            Map<String, Object> provinceMap = new HashMap<>();
            provinceMap.put("name", entry.getKey());
            provinceMap.put("children", cityData);

            transformedData.add(provinceMap);
        }

        return transformedData;
    }

    /**
     * 查询线索用户资质信息列表
     * 
     * @param sourceMemberDataInfoHistory 线索用户资质信息
     * @return 线索用户资质信息
     */
    @Override
    public List<SourceMemberDataInfoHistory> selectSourceMemberDataInfoHistoryList(SourceMemberDataInfoHistory sourceMemberDataInfoHistory)
    {
        return sourceMemberDataInfoHistoryMapper.selectSourceMemberDataInfoHistoryList(sourceMemberDataInfoHistory);
    }

    /**
     * 查询线索用户资质信息列表
     *
     * @param sourceMemberDataInfoHistory 线索用户资质信息
     * @return 线索用户资质信息
     */
    @Override
    public List<Map<String, Object>> selectSourceMemberDataInfoHistoryMapList(SourceMemberDataInfoHistory sourceMemberDataInfoHistory)
    {
        return sourceMemberDataInfoHistoryMapper.selectSourceMemberDataInfoHistoryMapList(sourceMemberDataInfoHistory);
    }

    /**
     * 新增线索用户资质信息
     * 
     * @param sourceMemberDataInfoHistory 线索用户资质信息
     * @return 结果
     */
    @Override
    public int insertSourceMemberDataInfoHistory(SourceMemberDataInfoHistory sourceMemberDataInfoHistory)
    {
        sourceMemberDataInfoHistory.setCreateTime(DateUtils.getNowDate());
        return sourceMemberDataInfoHistoryMapper.insertSourceMemberDataInfoHistory(sourceMemberDataInfoHistory);
    }

    /**
     * 修改线索用户资质信息
     * 
     * @param sourceMemberDataInfoHistory 线索用户资质信息
     * @return 结果
     */
    @Override
    public int updateSourceMemberDataInfoHistory(SourceMemberDataInfoHistory sourceMemberDataInfoHistory)
    {
        sourceMemberDataInfoHistory.setUpdateTime(DateUtils.getNowDate());
        return sourceMemberDataInfoHistoryMapper.updateSourceMemberDataInfoHistory(sourceMemberDataInfoHistory);
    }

    /**
     * 批量删除线索用户资质信息
     * 
     * @param ids 需要删除的线索用户资质信息主键
     * @return 结果
     */
    @Override
    public int deleteSourceMemberDataInfoHistoryByIds(Long[] ids)
    {
        return sourceMemberDataInfoHistoryMapper.deleteSourceMemberDataInfoHistoryByIds(ids);
    }

    /**
     * 删除线索用户资质信息信息
     * 
     * @param id 线索用户资质信息主键
     * @return 结果
     */
    @Override
    public int deleteSourceMemberDataInfoHistoryById(Long id)
    {
        return sourceMemberDataInfoHistoryMapper.deleteSourceMemberDataInfoHistoryById(id);
    }
}
